﻿using System;
using System.Data;

namespace AxeFrog.Data
{
	public static class IDbConnectionExtensions
	{
		public delegate IDbCommand ParamsHandler<T>(IDbCommand cmd, T entity);

		public static IDbCommand CreateCommand(this IDbConnection conn, string commandText)
		{
			var cmd = conn.CreateCommand();
			cmd.CommandText = commandText;
			return cmd;
		}

		public static IDbCommand CreateCommand(this IDbConnection conn, string commandText, CommandType commandType)
		{
			var cmd = conn.CreateCommand();
			cmd.CommandText = commandText;
			cmd.CommandType = commandType;
			return cmd;
		}

		public static IDbCommand CreateCommand<T>(this IDbConnection conn, string commandText, T entity, ParamsHandler<T> addParameters)
		{
			return addParameters(conn.CreateCommand(commandText), entity);
		}

		public static IDbCommand CreateCommand<T>(this IDbConnection conn, string commandText, CommandType commandType, T entity, ParamsHandler<T> addParameters)
		{
			return addParameters(conn.CreateCommand(commandText, commandType), entity);
		}

		public static IDbCommand CreateProcedure(this IDbConnection conn, string procName)
		{
			var cmd = conn.CreateCommand();
			cmd.CommandText = procName;
			cmd.CommandType = CommandType.StoredProcedure;
			return cmd;
		}

		public static IDbCommand CreateProcedure<T>(this IDbConnection conn, string procName, T entity, ParamsHandler<T> addParameters)
		{
			return addParameters(conn.CreateProcedure(procName), entity);
		}
	}
}